home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_09 / barbu2 / str.cpp < prev    next >
C/C++ Source or Header  |  1995-03-12  |  4KB  |  264 lines

  1. /////////////////////////////////////////////////////
  2. //    STR.CPP    Null-Ended String Class Implementation
  3. /////////////////////////////////////////////////////
  4. #include "STR.HPP"
  5. #include <ctype.h>
  6.  
  7. //////////////////////////////////////////////////////
  8. // Constructors:
  9. //////////////////////////////////////////////////////
  10. STR::STR()
  11. {
  12. if(0 == (s = new char[1])){
  13.     l = 0;
  14.     s = 0;
  15.     }
  16. else{
  17.     l = 1;
  18.     s[0] = '\0';
  19.     }
  20. }
  21.  
  22. STR::STR(const STR &y)
  23. {
  24. if(0 == (s = new char[y.l])){
  25.     l = 0;
  26.     s = 0;
  27.     }
  28. else{
  29.     l = y.l;
  30.     strcpy(s, y.s);
  31.     }
  32. }
  33.  
  34. STR::STR(const char str[])
  35. {
  36. int len;
  37. if(str == 0){
  38.     if(0 == (s = new char[1])){
  39.         l = 0;
  40.         s = 0;
  41.         }
  42.     else{
  43.         l = 1;
  44.         s[0]='\0';
  45.         }
  46.     return;
  47.     }
  48. if(0 == (s = new char[len = strlen(str)+1])){
  49.     l = 0;
  50.     s = 0;
  51.     }
  52. else{
  53.     l = len;
  54.     strcpy(s,str);
  55.     }
  56. }
  57.  
  58. STR::STR(int n)
  59. {
  60. if(0 == (s = new char[n+1])){
  61.     l = 0;
  62.     s = 0;
  63.     }
  64. else{
  65.     l = n+1;
  66.     s[0] = '\0';
  67.     }
  68. }
  69.  
  70. //////////////////////////////////////////////////////
  71. // Destructor:
  72. STR::~STR()
  73. {
  74. if(l != 0 && s != 0){
  75.     delete  s;
  76.     s = 0;
  77.     l = 0;
  78.     }
  79. }
  80.  
  81. STR& STR::operator=(const STR &y)
  82. {
  83. if(this == &y)
  84.     return *this;
  85. if(s != 0)
  86.     delete s;
  87. if(0 == (s = new char[y.l])){
  88.     l = 0;
  89.     s = 0;
  90.     }
  91. else{
  92.     l = y.l;
  93.     strcpy(s, y.s);
  94.     }
  95. return *this;
  96. }
  97.  
  98. STR& STR::operator+=(const STR &y)
  99. {
  100. if(strlen(s)+strlen(y.s) < l){
  101.     strcat(s, y.s);
  102.     return *this;
  103.     }
  104. STR tmpx(*this);
  105. STR tmpy(y);
  106. if(s != 0)
  107.     delete s;
  108. if(0 == (s = new char[l = tmpx.l + strlen(tmpy.s)])){
  109.     l = 0;
  110.     s = 0;
  111.     return *this;
  112.     }
  113. strcpy(s,tmpx.s);
  114. strcat(s,tmpy.s);
  115. return *this;
  116. }
  117.  
  118. STR& STR::operator=(const char str[])
  119. {
  120. if(s == str)
  121.     return *this;
  122. if(s != 0)
  123.     delete s;
  124. if(str == 0){
  125.     if(0 == (s = new char[1])){
  126.         l = 0;
  127.         s = 0;
  128.         }
  129.     else{
  130.         l = 1;
  131.         s[0]='\0';
  132.         }
  133.     return *this;
  134.     }
  135. if(0 == (s = new char[l = 1 + strlen(str)]))
  136.     l = 0;
  137. else
  138.     strcpy(s, str);
  139. return *this;
  140. }
  141.  
  142. STR& STR::operator+=(const char str[])
  143. {
  144. if(strlen(s)+strlen(str) < l){
  145.     strcat(s, str);
  146.     return *this;
  147.     }
  148. STR tmp(*this);
  149. if(s != 0)
  150.     delete s;
  151. if(0 == (s = new char[l = tmp.l + strlen(str)])){
  152.     l = 0;
  153.     s = 0;
  154.     return *this;
  155.     }
  156. strcpy(s,tmp.s);
  157. strcat(s,str);
  158. return *this;
  159. }
  160.  
  161. STR& STR::operator=(char c)
  162. {
  163. if(s != 0)
  164.     delete s;
  165. if(0 == (s = new char[l=2])){
  166.     l = 0;
  167.     s = 0;
  168.     }
  169. else{
  170.     s[0] = c;
  171.     s[1] = '\0';
  172.     }
  173. return *this;
  174. }
  175.  
  176. STR& STR::operator+=(char c)
  177. {
  178. int i;
  179. if(1 + (i=strlen(s)) < l){
  180.     s[i] = c;
  181.     s[i+1] = '\0';
  182.     return *this;
  183.     }
  184. STR tmp(*this);
  185. if(s != 0)
  186.     delete s;
  187. if(0 == (s = new char[l = tmp.l + 1])){
  188.     l = 0;
  189.     s = 0;
  190.     return *this;
  191.     }
  192. strcpy(s,tmp.s);
  193. s[l-2] = c;
  194. s[l-1] = '\0';
  195. return *this;
  196. }
  197.  
  198. int STR::streq(const char szS[]) const
  199. {
  200. for(int i=0; s[i] && szS[i]; i++)
  201.     if(::toupper(s[i]) != ::toupper(szS[i]))
  202.         return 0;
  203. return(s[i] || szS[i] ? 0 : 1);
  204. }
  205.  
  206. int STR::hasin(const char subs[], int nFromIndex,
  207.                 int bIgnoreCase) const
  208. {
  209. int subslen = strlen(subs);
  210. int searchlen = len() - subslen ;
  211. if(searchlen < 0)
  212.     return (-1);
  213. for(int i = nFromIndex; i <= searchlen; i++){
  214.     int bOk = 1;
  215.     for(int j = 0 ; j < subslen ; j++){
  216.         int c1 = s[i+j];
  217.         int c2 = subs[j];
  218.         if(bIgnoreCase){
  219.             c1 = ::toupper(c1);
  220.             c2 = ::toupper(c2);
  221.             }
  222.         if(c1 != c2){
  223.             bOk=0;
  224.             break;
  225.             }
  226.         }
  227.     if(bOk)
  228.         return i;
  229.     }
  230. return -1;
  231. }
  232.  
  233. int STR::hasin(char c, int nFromIndex,
  234.                 int bIgnoreCase) const
  235. {
  236. for(int i = nFromIndex; s[i]; i++){
  237.     if(s[i] == c)
  238.         return i;
  239.     if(bIgnoreCase && ::toupper(s[i]) == ::toupper(c))
  240.         return i;
  241.     }
  242. if(c=='\0')
  243.     return i;
  244. return -1;
  245. }
  246.  
  247. void STR::noFrontSpace()
  248. {
  249. int k,i;
  250. for(i = 0; isspace(s[i]); i++)
  251.     ;
  252. if(i == 0)
  253.     return;
  254. for(k = 0; s[i]; )
  255.     s[k++] = s[i++];
  256. s[k] = '\0';
  257. }
  258.  
  259. void STR::noTrailSpace()
  260. {
  261. for(int i = strlen(s)-1; i >= 0 && isspace(s[i]); i--)
  262.     s[i]='\0';
  263. }
  264.